home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / scherz programme / clicker / source / gui.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  8KB  |  261 lines

  1. /*  File:         gui.c
  2.  *  Created:      20-10-95
  3.  *  Updated:      30-12-95
  4.  *  Version:      1.0
  5.  *  Project:      Clicker
  6.  *  Owner:        Jeroen Vermeulen
  7.  *  Requirements: KickStart V39+
  8.  *  Legal:        PD
  9.  *  Status:       Release
  10.  */
  11.  
  12. #include <proto/exec.h>
  13. #include <exec/memory.h>
  14. #include <proto/graphics.h>
  15. #include <proto/intuition.h>
  16. #include <proto/gadtools.h>
  17. #include <intuition/gadgetclass.h>
  18.  
  19. #include "gui.h"
  20. #include "sound.h"
  21. #include "prefs.h"
  22.  
  23.  
  24. static STRPTR
  25.         AllocFailWindow = "Couldn't allocate memory for prefs window!\n",
  26.         OpenFailWindow  = "Couldn't open prefs window!\n",
  27.         LockFailPubScr  = "Couldn't lock public screen\n",
  28.         GetFailVisInfo  = "Couldn't get VisualInfo for screen\n",
  29.         AllocFailGadget = "Unable to create gadgets for prefs window!\n";
  30.  
  31.  
  32. /* Topaz-sensitive TextAttr structure.
  33.  * The catchphrase "Topaze-sensitive" is a trademark of Hans Guijt.
  34.  */
  35. static struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0 };
  36.  
  37.  
  38. /* Constant GUI tags
  39.  */
  40. static const struct TagItem StaticFrequencyTags[] =
  41. {
  42.   {GTSL_Min,         -5*12},
  43.   {GTSL_Max,         4*12},
  44.   {GTSL_LevelFormat, (ULONG)"%4ld"},
  45.   {GTSL_LevelPlace,  PLACETEXT_RIGHT},
  46.   {GTSL_DispFunc,    (ULONG)SliderToHertz},
  47.   {GTSL_MaxLevelLen, 4},
  48.   {GA_Immediate,     TRUE},
  49.   {TAG_END,          NULL}
  50. },
  51. StaticVolumeTags[] =
  52. {
  53.   {GTSL_Max,         64},
  54.   {GTSL_LevelFormat, (ULONG)"%2ld"},
  55.   {GTSL_LevelPlace,  PLACETEXT_RIGHT},
  56.   {GTSL_MaxLevelLen, 4},
  57.   {GA_Immediate,     TRUE},
  58.   {TAG_END,          NULL}
  59. },
  60. StaticCycleTags[] =
  61. {
  62.   {GTSL_Min,         1},
  63.   {GTSL_Max,         20},
  64.   {GTSL_LevelFormat, (ULONG)"%2ld"},
  65.   {GTSL_LevelPlace,  PLACETEXT_RIGHT},
  66.   {GTSL_MaxLevelLen, 4},
  67.   {GA_Immediate,     TRUE},
  68.   {TAG_END,          NULL}
  69. },
  70. StaticPrefsWindowTags[] =
  71. {
  72.   {WA_Width,       250},
  73.   {WA_Height,      100},
  74.   {WA_Title,       (ULONG)"Clicker Prefs"},
  75.   {WA_Flags,       WFLG_DRAGBAR | WFLG_DEPTHGADGET |
  76.                    WFLG_CLOSEGADGET | WFLG_SMART_REFRESH |
  77.                    WFLG_ACTIVATE | WFLG_NEWLOOKMENUS},
  78.   {WA_IDCMP,       SLIDERIDCMP | CHECKBOXIDCMP | BUTTONIDCMP |
  79.                    IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW},
  80.   {TAG_END,        NULL}
  81. };
  82.  
  83.  
  84.  
  85. /* MakeWindow():
  86.  * Sets up the prefs window, but doesn't show it unless the Show argument is
  87.  * TRUE.  If successful, MakeWindow() returns a pointer to a WindowContext
  88.  * structure.  If not, NULL is returned and the STRPTR pointed to by errptr will
  89.  * point to an error string.
  90.  * The window must later be deallocated with DestroyWindow().
  91.  */
  92. struct WindowContext *MakeWindow(STRPTR *const errptr, const BOOL Show)
  93. {
  94.   struct WindowContext *WinStruct;
  95.   /* --- */
  96.   if ((WinStruct = AllocMem(sizeof(struct WindowContext),MEMF_ANY|MEMF_CLEAR)))
  97.   {
  98.     if (Show) ShowWindow(errptr,WinStruct);
  99.   }
  100.   else *errptr = AllocFailWindow;
  101.  
  102.   return WinStruct;
  103. }
  104.  
  105. /* ShowWindow():
  106.  * Reveal prefs window, setting it up first if necessary.  A non-NULL pointer to
  107.  * its WindowContext structure is passed in as an argument.
  108.  * If the window fails to open, errptr will point to an error string.
  109.  */
  110. void ShowWindow(STRPTR *const errptr, struct WindowContext *const WinStruct)
  111. {
  112.   if (!WinStruct->Shown)
  113.   {
  114.     struct Screen *pubscr;
  115.     if (!(WinStruct->pubscr = pubscr = LockPubScreen(NULL)))
  116.     {
  117.       *errptr = LockFailPubScr;
  118.       return;
  119.     }
  120.     if ((WinStruct->visinfo = GetVisualInfoA(pubscr,NULL)))
  121.     {
  122.       struct Gadget *gad;
  123.       /* --- */
  124.       if ((gad = CreateContext(&WinStruct->glist)))
  125.       {
  126.         struct TagItem ClickMouseTags[] =
  127.         {
  128.           {GTCB_Checked,   FALSE},  /* Set to ClickPrefs.ClickMouse later! */
  129. #ifdef    NOCLICKMOUSE
  130.           {GA_Disabled,    TRUE},   /* Mouse click permanently disabled */
  131. #endif /* NOCLICKMOUSE */
  132.           {TAG_END,        NULL}
  133.         };
  134.  
  135.         struct NewGadget gadget =
  136.         {
  137.           10, 0, 26, 11,
  138.           "Click Mouse button",
  139.           &Topaz80,
  140.           mygadget_clickmouse,
  141.           PLACETEXT_RIGHT,
  142.           NULL,
  143.           NULL
  144.         };
  145.  
  146.         /* --- */
  147.  
  148.         gadget.ng_TopEdge = 12 + pubscr->WBorTop + pubscr->Font->ta_YSize+1;
  149.         gadget.ng_VisualInfo = WinStruct->visinfo;
  150.  
  151. #ifndef   NOCLICKMOUSE
  152.         ClickMouseTags[0].ti_Data = ClickPrefs.ClickMouse;
  153. #endif /* NOCLICKMOUSE */
  154.  
  155.         gad = CreateGadgetA(CHECKBOX_KIND, gad, &gadget,ClickMouseTags);
  156.  
  157.         gadget.ng_Width = 100;
  158.         gadget.ng_LeftEdge = 90;
  159.         gadget.ng_Flags = 0;
  160.  
  161.         gadget.ng_TopEdge += 12;
  162.         gadget.ng_GadgetText = "Pitch";
  163.         gadget.ng_GadgetID = mygadget_period;
  164.  
  165.         /* The gadget is supposed to show frequency in Hz, but the actual slider
  166.          * positions are logarithmic.  Hitting the A at 440 Hz is the hard one.
  167.          * A 12-tone scale over 9 octaves is used; frequency can be determined
  168.          * from the slider position by the formula
  169.          *
  170.          *                      Freq = 440 * 2^(n/12)
  171.          *
  172.          * It thus ranges from the A at 13.75 Hz to the one at 7040 Hz.  This
  173.          * conversion is performed by SliderToHertz().
  174.          */
  175.         gad = CreateGadget(SLIDER_KIND, gad, &gadget,
  176.                            GTSL_Level,     PeriodToSlider(ClickPrefs.period),
  177.                            TAG_MORE,       (ULONG)StaticFrequencyTags);
  178.  
  179.         gadget.ng_TopEdge += 12;
  180.         gadget.ng_GadgetText = "Volume";
  181.         gadget.ng_GadgetID = mygadget_volume;
  182.  
  183.         gad = CreateGadget(SLIDER_KIND, gad, &gadget,
  184.                            GTSL_Level,     ClickPrefs.volume,
  185.                            TAG_MORE,       (ULONG)StaticVolumeTags);
  186.  
  187.         gadget.ng_TopEdge += 12;
  188.         gadget.ng_GadgetText = "Length";
  189.         gadget.ng_GadgetID = mygadget_cycles;
  190.  
  191.         gad = CreateGadget(SLIDER_KIND, gad, &gadget,
  192.                            GTSL_Level,     ClickPrefs.cycles,
  193.                            TAG_MORE,       (ULONG)StaticCycleTags);
  194.  
  195.         if (gad)
  196.         {
  197.           WinStruct->Win = OpenWindowTags(NULL,
  198.                                  WA_Gadgets,    (ULONG)WinStruct->glist,
  199.                                  TAG_MORE,      (ULONG)StaticPrefsWindowTags);
  200.           if (WinStruct->Win)
  201.           {
  202.             GT_RefreshWindow(WinStruct->Win,NULL);
  203.             WinStruct->Shown = TRUE;
  204.             WinStruct->SigMask = 1L<<WinStruct->Win->UserPort->mp_SigBit;
  205.           }
  206.           else
  207.           {
  208.             FreeGadgets(gad);
  209.             gad = WinStruct->glist = NULL;
  210.             *errptr = OpenFailWindow;
  211.           }
  212.         }
  213.         else *errptr = AllocFailGadget;
  214.       }
  215.       if (!gad)
  216.       {
  217.         FreeVisualInfo(WinStruct->visinfo);
  218.         WinStruct->visinfo = NULL;
  219.       }
  220.     }
  221.     else *errptr = GetFailVisInfo;
  222.     if (!WinStruct->visinfo)
  223.     {
  224.       UnlockPubScreen(NULL,pubscr);
  225.       WinStruct->pubscr = NULL;
  226.     }
  227.   }
  228. }
  229.  
  230.  
  231. /* HideWindow():
  232.  * Removes the prefs window from the screen, but may keep its structures around
  233.  * for speed and convenience.
  234.  */
  235. void HideWindow(struct WindowContext *const WinStruct)
  236. {
  237.   if (WinStruct && WinStruct->Shown)
  238.   {
  239.     CloseWindow(WinStruct->Win);
  240.     FreeGadgets(WinStruct->glist);
  241.     FreeVisualInfo(WinStruct->visinfo);
  242.     UnlockPubScreen(NULL,WinStruct->pubscr);
  243.     WinStruct->Shown = FALSE;
  244.     WinStruct->SigMask = 0;
  245.     WinStruct->visinfo = NULL;
  246.     WinStruct->glist = NULL;
  247.     WinStruct->pubscr = NULL;
  248.   }
  249. }
  250.  
  251.  
  252. /* DestroyWindow():
  253.  * Call this to deallocate the prefs window.  It will be closed first if
  254.  * necessary.
  255.  */
  256. void DestroyWindow(struct WindowContext *const WinStruct)
  257. {
  258.   HideWindow(WinStruct);
  259. }
  260.  
  261.